package cn.sunny360.messsage.impl.json;

import cn.sunny360.messsage.MsgMessage;
import com.alibaba.fastjson.JSONObject;

import java.nio.charset.StandardCharsets;

public class JsonMsgMessage implements MsgMessage<JsonMsgMessage> {
    private JSONObject content;

    public JsonMsgMessage(){
        content = new JSONObject();
    }

    public JsonMsgMessage(String json){
        content = JSONObject.parseObject(json);
    }

    public JsonMsgMessage(JSONObject jsonObject) {
        content = jsonObject;
    }

    @Override
    public String toJson() {
        return content.toJSONString();
    }

    @Override
    public JsonMsgMessage parseJson(String s) {
        content = JSONObject.parseObject(s);
        return this;
    }

    @Override
    public byte[] toByteArray() {
        return content.toJSONString().getBytes(StandardCharsets.UTF_8);
    }

    @Override
    public JsonMsgMessage parseByteArray(byte[] bytes) {
        content = JSONObject.parseObject(new String(bytes, StandardCharsets.UTF_8));
        return this;
    }

    @Override
    public String getType() {
        return content.getString("type");
    }

    @Override
    public long getVersion() {
        return content.getLong("version");
    }

    @Override
    public String getTime() {
        return content.getString("time");
    }

    @Override
    public String getFrom() {
        return content.getString("from");
    }

    @Override
    public String getTo() {
        return content.getString("to");
    }

    @Override
    public String getGroup() {
        return content.getString("group");
    }

    @Override
    public String getToken() {
        return content.getString("token");
    }

    @Override
    public int getContentType() {
        return content.getIntValue("content_type");
    }

    @Override
    public byte[] getContent() {
        return content.getBytes("content");
    }

    @Override
    public int getAttachType() {
        return content.getIntValue("attach_type");
    }

    @Override
    public String getAttachment() {
        return content.getString("attachment");
    }

    @Override
    public String getEndTime() {
        return content.getString("end_time");
    }

    @Override
    public MsgMessage setType(String type) {
        content.put("type", type);
        return this;
    }

    @Override
    public MsgMessage setVersion(long version) {
        content.put("version", version);
        return this;
    }

    @Override
    public MsgMessage setTime(String time) {
        content.put("time", time);
        return this;
    }

    @Override
    public MsgMessage setFrom(String from) {
        content.put("from", from);
        return this;
    }

    @Override
    public MsgMessage setTo(String to) {
        content.put("to", to);
        return this;
    }

    @Override
    public MsgMessage setGroup(String group) {
        content.put("group", group);
        return this;
    }

    @Override
    public MsgMessage setToken(String token) {
        content.put("token", token);
        return null;
    }

    @Override
    public MsgMessage setContentType(int contentType) {
        content.put("content_type", contentType);
        return this;
    }

    @Override
    public MsgMessage setContent(byte[] content) {
        this.content.put("content",content);
        return this;
    }

    @Override
    public MsgMessage setAttachType(int attachType) {
        content.put("attach_type", attachType);
        return this;
    }

    @Override
    public MsgMessage setAttachment(String attachment) {
        content.put("attachment", attachment);
        return this;
    }

    @Override
    public MsgMessage setEndTime(String endTime) {
        content.put("end_time", endTime);
        return this;
    }
}
